package org.loadsavant.task.parse;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.EmptyVisitor;
import org.springframework.util.ClassUtils;

public class AsmTaskVisitor extends EmptyVisitor {
	private String className;
	private boolean abstractClass;
	private boolean hasTaskAnnotation = false;

	@Override
	public void visit(int v, int access, String name, String signature,
			String superName, String[] interfaces) {
		
		className = ClassUtils.convertResourcePathToClassName(name);
		abstractClass = (access & Opcodes.ACC_ABSTRACT) != 0;
	}

	@Override
	public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
		if (Type.getDescriptor(
				TaskDefinitionParser.TASK_ANNOTATION_TYPE).equals(desc)) {
			hasTaskAnnotation = true;
		}
		
		return super.visitAnnotation(desc, visible);
	}

	public boolean isAbstract() {
		return abstractClass;
	}
	
	public boolean isValidTask() {
		return !isAbstract() && hasTaskAnnotation;
	}
	
	public String getClassName() {
		return className;
	}
}
